const express = require('express')
const router = express.Router()
const {
    initData,
    insertProduct,
    readProduct,
    getCategoryProductList,
    removeProductImg,
    removeProductList,
    modifyProductList,
    updateProductListState,
    getProductListByState, getProductDetail,
} = require('../../model/mongo')

const multipart = require('../../middleware/content-type/multypart')

router.use(multipart('./server/img/shop'))

router.get('/init', async (req, res) => {
    let result = {
        code: 200,
        data: []
    }
    try {
        result.data = await initData()
    } catch (err) {
        result.code = 400
        result.msg = err
    }
    res.send(JSON.stringify(result))
})

router.get('/list', (req, res) => {
    readProduct().then(data => {
        res.send(JSON.stringify({
            code: 200,
            msg: '获取全部商品列表',
            data,
        }))
    }).catch(err => {
        res.send(JSON.stringify({
            code: 400,
            msg: '获取商品失败',
            err,
        }))
    })
})

router.get('/detail/:proid', async (req, res) => {
    const { proid } = req.params
    const result = {
        code: 200,
        msg: '获取商品详情'
    }
    result.data = await getProductDetail(proid)
    res.send(JSON.stringify(result))
})

router.post('/add', (req, res) => {
    let {
        brand,
        category,
        proname,
        originprice,
        discount,
        stock,
        desc,
        fileList,
        swiper
    } = req.body

    originprice = Number(originprice)
    discount = Number(discount)
    stock = Number(stock)
    // 兼容传字符串和form-data文件
    swiper = fileList && fileList.map(item => item.path) || swiper

    insertProduct({
        brand,
        category,
        proname,
        originprice,
        discount,
        stock,
        desc,
        swiper
    }).then(suc => {
        res.send(JSON.stringify({
            code: 200,
            msg: '商品添加成功',
            data: suc
        }))
    }).catch(err => {
        res.send(JSON.stringify({
            code: 400,
            msg: '添加商品失败-请稍后重试'
        }))
    })
})

router.put('/modify', async (req, res) => {
    const { proid, data } = req.body
    const result = {
        code: 200,
        msg: ''
    }
    try {
        result.msg = await modifyProductList(proid, data)
        result.data = data
    } catch (err) {
        result.code = 400
        result.msg = err
    }
    res.send(JSON.stringify(result))
})

router.delete('/remove', async (req, res) => {
    const { proid } = req.body
    const result = {
        code: 200,
        msg: '',
    }
    try {
        result.msg = await removeProductList(proid)
    } catch (err) {
        result.code = 404
        result.msg = err
    }
    res.send(JSON.stringify(result))
})

router.post('/removeImg', async (req, res) => {
    let { img, proid } = req.body
    let result = {
        code: 200,
        msg: ''
    }
    try {
        result.msg = await removeProductImg({ proid, img })
    } catch (e) {
        result.msg = e
    }
    res.send(JSON.stringify(result))
})

router.get('/showdata', async (req, res) => {
    let { type, flag } = req.query
    let result = {
        code: 200,
        msg: '获取' + type + '状态下数据',
        data: []
    }
    if (flag === 'true') {
        flag = true
    }
    if (flag === 'false') {
        flag = false
    }
    try {
        result.data = await getProductListByState(type, flag)
    } catch (err) {
        result.code = 400
        result.msg = err
    }
    res.send(JSON.stringify(result))
})

router.post('/updateFlag', async (req, res) => {
    let { proid, type, flag } = req.body
    let result = {
        code: 200,
        msg: ''
    }
    if (flag === 'true') {
        flag = true
    }
    if (flag === 'false') {
        flag = false
    }
    try {
        if (type === 'issale' && flag === false) {
            result.msg = await updateProductListState(proid, 'isrecommend', flag)
            result.msg = await updateProductListState(proid, 'isseckill', flag)
        }
        if ((type === 'isrecommend' && flag === true) || (type === 'isseckill' && flag === true)) {
            result.msg = await updateProductListState(proid, 'issale', flag)
        }
        result.msg = await updateProductListState(proid, type, flag)
    } catch (err) {
        result.code = 400
        result.msg = err
    }
    res.send(JSON.stringify(result))
})

router.get('/getListByCategory', async (req, res) => {
    let { category } = req.query
    const result = {
        code: 200,
        msg: '获取' + category + '分类下的全部列表',
        data: {}
    }
    try {
        result.data = await getCategoryProductList(category)
    } catch (err) {
        result.code = 400
    }
    res.send(JSON.stringify(result))
})

module.exports = router
